﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YJKCodeBase.WinForm;

namespace YJKAxisAndEdit
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdGridTagShowHide : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			this.m_uiDoc = cmdData.Application.ActiveUIDocument;
			this.m_Doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_app = cmdData.Application.Application;
			this.m_selection = cmdData.Application.ActiveUIDocument.Selection;
			RevitVersionFuncs.ClearSelection(this.m_selection);
			if (CmdGridTagShowHide._hWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
				CmdGridTagShowHide._hWndRevit = new WindowHandle(mainWindowHandle);
			}
			this.LoadFamily();
			GridTagShowHideFrm gridTagShowHideFrm = new GridTagShowHideFrm(cmdData);
			gridTagShowHideFrm.Show(CmdGridTagShowHide._hWndRevit);
			AxisFilter axisFilter = new AxisFilter();
			int wMsg = 7;
			CmdGridTagShowHide.SendMessage(CmdGridTagShowHide._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			Result result;
			for (;;)
			{
				List<Element> list = null;
					
            IL_B8:
				try
				{
					list = new List<Element>();
					if (gridTagShowHideFrm.SelMode == 1)
					{
						Reference reference = this.m_selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, axisFilter, "请选择轴线:");
						list.Add(this.m_Doc.GetElement(reference));
					}
					else
					{
						list = (this.m_uiDoc.Selection.PickElementsByRectangle(axisFilter, "请选择轴线:") as List<Element>);
					}
					if (list.Count == 0)
					{
						continue;
					}
				}
				catch (Exception)
				{
					if (gridTagShowHideFrm.m_clickModeBtn)
					{
						gridTagShowHideFrm.m_clickModeBtn = false;
						goto IL_B8;
					}
					gridTagShowHideFrm.Close();
					result = Autodesk.Revit.UI.Result.Succeeded;
					break;
				}
				RequestParam requestParam = default(RequestParam);
				requestParam.m_commandData = cmdData;
				requestParam.m_Frm = gridTagShowHideFrm;
				requestParam.m_lstGrid = new List<Element>();
				requestParam.m_lstGrid.AddRange(list);
				HideOrShowGridTag hideOrShowGridTag = new HideOrShowGridTag();
				hideOrShowGridTag.DoRequest(requestParam);
			}
			return result;
		}

		private void LoadFamily()
		{
			bool flag = false;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
			filteredElementCollector.OfClass(typeof(Family)).ToElements();
			foreach (Element element in filteredElementCollector)
			{
				Family family = (Family)element;
				if (family.Name.CompareTo("Grid_Head_Null") == 0)
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				string text = AssistFunc.GetAppRootPath();
				string versionNumber = this.m_app.VersionNumber;
				text = text + "\\Templates\\" + versionNumber + "\\Grid_Head_Null.rfa";
				if (!File.Exists(text))
				{
					throw new Exception("Unsupported version");
				}
				Transaction transaction = new Transaction(this.m_Doc);
				transaction.Start("load families");
				this.m_Doc.LoadFamily(text);
				transaction.Commit();
			}
		}

		public const int HideOrShowRequestId = 10;

		public static WindowHandle _hWndRevit;

		private UIDocument m_uiDoc;

		private Document m_Doc;

		private Application m_app;

		private Selection m_selection;
	}
}
